﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// Enum类型转换接口
    /// </summary>
    public class EnumTypeConverter : TypeConverterAbs
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        public EnumTypeConverter()
            : base(PrimitiveDataTypes.EnumType)
        {

        }


        /// <summary>
        /// 将字符串转换为值类型数据,包括枚举
        /// </summary>
        /// <param name="options">类型转换选项参数</param>
        /// <param name="value">要转换的值</param>
        /// <param name="valueType">值类型</param>
        /// <returns>转换后的值</returns>
        protected override object PrimitiveConvertTo(ITypeConverterOptions options, object value, PrimitiveDataTypes valueType)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options), "当目标数据类型为枚举时,转换选项必须指定枚举解析器类型");
            }

            IEnumParser enumParser;
            object result;

            switch (valueType)
            {
                case PrimitiveDataTypes.StringType:
                    enumParser = EnumParserManager.GetEnumParser(options.TargetEnumTypeName);
                    if (!enumParser.TryParse((string)value, out result))
                    {
                        throw new InvalidCastException($"类型\"{EnumParserManager.PrimitiveTypesEnumParser.GetEnumString(valueType)}\"转换为\"{options.TargetEnumTypeName}\"失败");
                    }
                    break;
                default:
                    enumParser = EnumParserManager.GetEnumParser(options.TargetEnumTypeName);
                    if (enumParser.EnumNumberType != valueType)
                    {
                        value = ConvertEx.ConvertTo(value, enumParser.EnumNumberType, null);
                    }

                    if (!enumParser.TryParse(value, out result))
                    {
                        throw new InvalidCastException($"类型\"{EnumParserManager.PrimitiveTypesEnumParser.GetEnumString(valueType)}\"不支持转换为类型\"{options.TargetEnumTypeName}\",转换失败");
                    }
                    break;
            }
            return result;
        }

        /// <summary>
        /// 转换数据到
        /// </summary>
        /// <param name="options">类型转换选项参数</param>
        /// <param name="value">要转换的值</param>
        /// <param name="valueType">值类型</param>
        /// <param name="result">转换后的值,存放在object中,如果转换失败为目标类型的默认值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(ITypeConverterOptions options, object value, PrimitiveDataTypes valueType, out object result)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options), "当目标数据类型为枚举时,转换选项必须指定枚举解析器类型");
            }

            bool parseRet = true;
            IEnumParser enumParser;

            switch (valueType)
            {
                case PrimitiveDataTypes.StringType:
                    if (EnumParserManager.TryGetEnumParser(options.TargetEnumTypeName, out enumParser))
                    {
                        if (enumParser.TryParse((string)value, out result))
                        {

                        }
                        else
                        {
                            //throw new InvalidCastException($"类型\"{EnumParserHelper.PrimitiveTypesEnumParser.GetEnumString(valueType)}\"转换为\"{options.TargetEnumTypeName}\"失败");
                            result = null;
                            parseRet = false;
                        }
                    }
                    else
                    {
                        //throw new InvalidCastException($"枚举类型\"{EnumParserHelper.PrimitiveTypesEnumParser.GetEnumString(valueType)}\"解析器不存在");
                        result = null;
                        parseRet = false;
                    }
                    break;
                default:
                    enumParser = EnumParserManager.GetEnumParser(options.TargetEnumTypeName);
                    if (enumParser.EnumNumberType != valueType)
                    {
                        value = ConvertEx.ConvertTo(value, enumParser.EnumNumberType, null);
                    }

                    if (!enumParser.TryParse(value, out result))
                    {
                        parseRet = false;
                    }
                    break;
            }

            return parseRet;
        }

    }
}
